package trade.axht.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import trade.axht.entity.Admin;

public class TestCase {
	
	@Test
	public void selectById_test() throws IOException{
		String resource="conf/mybatis-config.xml";
		//加载mybatis的配置文件（它也加载相关的映射文件）
		InputStream inputStream=TestCase.class.getClassLoader().getResourceAsStream(resource);
		//构建sqlSession的工厂
		SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
		//创建能执行映射文件中sql的sqlSession
		SqlSession session=sessionFactory.openSession();
		//映射sql的标识字符串
		String statement="trade.axht.entity.AdminMapper.getuser";
		//执行查询返回一个唯一admin对象的sql
		Admin admin=session.selectOne(statement,1);
		System.out.println(admin);

		
	}
	
	@Test
	public void insertTest(){
		String resource="conf/mybatis-config.xml";
		InputStream inputStream=TestCase.class.getClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session=sqlSessionFactory.openSession(true);
		String statement="trade.axht.entity.AdminMapper.addUser";
		int i=session.insert(statement);
		if(i>0){
			System.out.println("插入成功！");
		}
	
	}
	

	@Test
	public void insertByParameterTest(){
		String resource="conf/mybatis-config.xml";
		InputStream inputStream=TestCase.class.getClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session=sqlSessionFactory.openSession(true);
		String statement="trade.axht.entity.AdminMapper.addUserByParameter";
		Admin admin=new Admin();
		admin.setUsername("宇文成都");
		admin.setPasswords("yuwenchengdou");
		int i=session.insert(statement,admin);
		if(i>0){
			System.out.println("插入成功！");
		}
	
	}
	
	@Test
	public void findByIdTest(){
		String resource="conf/mybatis-config.xml";
		InputStream inputStream=TestCase.class.getClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session=sqlSessionFactory.openSession(true);
		String statement="trade.axht.entity.AdminMapper.findById";
		Admin admin=session.selectOne(statement, 4);
		System.out.println(admin);
	
	}
	
	@Test
	public void findAllTest(){
		String resource="conf/mybatis-config.xml";
		InputStream inputStream=TestCase.class.getClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session=sqlSessionFactory.openSession(true);
		String statement="trade.axht.entity.AdminMapper.findAll";
		List<Admin> admins=session.selectList(statement);
		System.out.println(admins);
	
	}
	
	
	@Test
	public void updateTest(){
		String resource="conf/mybatis-config.xml";
		InputStream inputStream=TestCase.class.getClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session=sqlSessionFactory.openSession(true);
		String statement="trade.axht.entity.AdminMapper.update";
		Admin admin=new Admin();
		admin.setUsername("宇文成都");
		admin.setId(1);
		admin.setPasswords("yuwenchengdou");
		int i=session.update(statement,admin);
		if(i>0){
			System.out.println("更新成功！");
		}
	
	}
	
	@Test
	public void deleteTest(){
		String resource="conf/mybatis-config.xml";
		InputStream inputStream=TestCase.class.getClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session=sqlSessionFactory.openSession(true);
		String statement="trade.axht.entity.AdminMapper.delete";
		Admin admin=new Admin();
		admin.setUsername("宇文成都");
		admin.setId(1);
		admin.setPasswords("yuwenchengdou");
		int i=session.delete(statement,admin);
		if(i>0){
			System.out.println("删除成功！");
		}
	
	}
	
	
	@Test
	public void findByPageTest(){
		String resource="conf/mybatis-config.xml";
		InputStream inputStream=TestCase.class.getClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session=sqlSessionFactory.openSession(true);
		String statement="trade.axht.entity.AdminMapper.findByPage";
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("pstart",1);
		map.put("psize", 2);
		List<Admin> admins=session.selectList(statement, map);
		System.out.println(admins);
	
	}
	
	@Test
	public void findAllByNameWithPageTest(){
		String resource="conf/mybatis-config.xml";
		InputStream inputStream=TestCase.class.getClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session=sqlSessionFactory.openSession(true);
		String statement="trade.axht.entity.AdminMapper.findAllByNameWithPage";
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("pstart",0);
		map.put("psize", 2);
		map.put("pname", "%LI%");
		List<Admin> admins=session.selectList(statement, map);
		System.out.println(admins);
	
	}
	
	@Test
	public void DynamicfindAllTest(){
		String resource="conf/mybatis-config.xml";
		InputStream inputStream=TestCase.class.getClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session=sqlSessionFactory.openSession(true);
		String statement="trade.axht.entity.AdminMapper.DynamicfindAll";
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("pid",3);
		map.put("pusername","天策");
		map.put("password", "tiance");
		List<Admin> admins=session.selectList(statement, map);
	
		System.out.println(admins);
	
	}
	
	@Test
	public void DynamicUpdateTest(){
		String resource="conf/mybatis-config.xml";
		InputStream inputStream=TestCase.class.getClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session=sqlSessionFactory.openSession(true);
		String statement="trade.axht.entity.AdminMapper.DynamicUpdate";
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("id",3);
		map.put("pname","天策之子");
		map.put("passwords", "tiancecccccc");
		int i=session.update(statement, map);
		if(i>0)
			System.out.println("更新成功！");
	
	}
	
	@Test
	public void DynamicDeleteByArray(){
		String resource="conf/mybatis-config.xml";
		InputStream inputStream=TestCase.class.getClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session=sqlSessionFactory.openSession(true);
		String statement="trade.axht.entity.AdminMapper.DynamicDeleteByArray";
		int [] ids={1,2,3};
		int i=session.delete(statement, ids);
		if(i>0)
			System.out.println("删除成功！"+i);
	
	}
	
	
	@Test
	public void DynamicDeleteByList(){
		String resource="conf/mybatis-config.xml";
		InputStream inputStream=TestCase.class.getClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session=sqlSessionFactory.openSession(true);
		String statement="trade.axht.entity.AdminMapper.DynamicDeleteByList";
		List<Integer> ids=new ArrayList<Integer>();
		ids.add(1);
		ids.add(4);
		int i=session.delete(statement, ids);
		if(i>0)
			System.out.println("删除成功！"+i);
	
	}
	
	@Test
	public void DynamicInsert(){
		String resource="conf/mybatis-config.xml";
		InputStream inputStream=TestCase.class.getClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session=sqlSessionFactory.openSession(true);
		String statement="trade.axht.entity.AdminMapper.DynamicInsert";
		
		for(int j=21;j<40;j++){
			Admin admin=new Admin();
		
			admin.setId(j);
			admin.setPasswords("yuwenchengdou");
			int i=session.insert(statement, admin);
			if(i>0)
				System.out.println("添加成功！"+i);
		}
		
	
	}
	
	
	
}
